'''
# Coding:utf-8
# Project: work-platform
# Author: rtf
# Time: 2021-02-23 11:40:00
# FileName: date_process.py
# Software: PyCharm
'''

import datetime
from datetime import timedelta


# 获取本周(指定日期)周一、周天的日期
# date: "2022-08-09"
def get_current_week(date=None):
    if date:
        duty_date = datetime.datetime.strptime(str(date), '%Y-%m-%d')
        monday, sunday = duty_date, duty_date
    else:
        monday, sunday = datetime.date.today(), datetime.date.today()
    one_day = datetime.timedelta(days=1)
    while monday.weekday() != 0:
        monday -= one_day
    while sunday.weekday() != 6:
        sunday += one_day

    # return monday, sunday
    # 返回时间字符串
    return datetime.datetime.strftime(monday, "%Y-%m-%d"), datetime.datetime.strftime(sunday, "%Y-%m-%d")


# 获取上周周一、周天的日期
# date: "2022-08-09"
def get_last_week(date=None):
    if date:
        today = datetime.datetime.strptime(str(date), '%Y-%m-%d')
    else:
        today = datetime.datetime.today()
    end_time = today - datetime.timedelta(days=today.isoweekday())
    start_time = end_time - datetime.timedelta(days=6)
    return start_time.strftime("%Y-%m-%d"), end_time.strftime("%Y-%m-%d")


# 获取下周周一、周天的日期
# date: "2022-08-09"
def get_next_week(date=None):
    if date:
        today = datetime.datetime.strptime(str(date), '%Y-%m-%d')
    else:
        today = datetime.datetime.today()
    start_time = today + datetime.timedelta(days=7-today.weekday())
    end_time = start_time + datetime.timedelta(days=6)
    return start_time.strftime("%Y-%m-%d"), end_time.strftime("%Y-%m-%d")
    

# 根据开始日期、结束日期返回这段时间里所有天的集合
def get_dates(start_date, end_date):
    dates = list()
    start_date = datetime.datetime.strptime(start_date, '%Y-%m-%d')
    end_date = datetime.datetime.strptime(end_date, '%Y-%m-%d')
    dates.append(start_date.strftime('%Y-%m-%d'))
    while start_date < end_date:
        start_date += datetime.timedelta(days=1)
        dates.append(start_date.strftime('%Y-%m-%d'))
    return dates


# 获取昨天
def get_yesterday():
    yesterday = datetime.date.today() - datetime.timedelta(days=1)
    return yesterday


def get_last_month_start_end():
    """
    获取上月第一天和最后一天并返回(元组)
    example:
        now date:2020-03-06
        return:2020-02-01,2020-02-29
    :return:
    """
    today = datetime.date.today()
    last_day_of_last_month = datetime.date(today.year, today.month, 1) - datetime.timedelta(1)
    first_day_of_last_month = datetime.date(last_day_of_last_month.year, last_day_of_last_month.month, 1)

    return first_day_of_last_month, last_day_of_last_month


def get_current_month_start_end():
    now = datetime.date.today()
    month_start = datetime.date(now.year, now.month, 1)
    if now.month < 12:
        month_end = datetime.date(now.year, now.month + 1, 1) - timedelta(days=1)
    else:
        month_end = datetime.date(now.year, now.month, 1) + timedelta(days=30)
    return month_start, month_end


def get_month_start_end(date):
    year = int(date[0:4])
    month = int(date[5:])
    month_start = datetime.date(year, month, 1)
    if month < 12:
        month_end = datetime.date(year, month + 1, 1) - timedelta(days=1)
    else:
        month_end = datetime.date(year, month, 1) + timedelta(days=30)
    return month_start, month_end


def last_day_of_month(any_day):
    """
    获取获得一个月中的最后一天
    :param any_day: 任意日期
    :return: string
    """
    next_month = any_day.replace(day=28) + datetime.timedelta(days=4)  # this will never fail
    return next_month - datetime.timedelta(days=next_month.day)


def get_diff_days(start_time, end_time):
    """
    获取两个日期相差的天数(请假忽略节假日)
    :param start_time:
    :param end_time:
    :return:
    """
    dates = get_dates(start_time[0:10], end_time[0:10])
    start_time = datetime.datetime.strptime(start_time, "%Y-%m-%d %H:%M:%S")
    end_time = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")
    # # 计算当月时间差，如果跨月，结束时间设置为当前月最后一天
    # if end_time.month != datetime.datetime.now().month and start_time.month == datetime.datetime.now().month:
    #     end_time = f"{last_day_of_month(datetime.datetime.now().date())} 23:59:59"
    #     end_time = datetime.datetime.strptime(end_time, "%Y-%m-%d %H:%M:%S")

    diff_days = (end_time - start_time).days if (end_time - start_time).days > 0 else 0
    return diff_days


'''
# Coding:utf-8
# Project: iiop
# Author: rtf
# Time: 2021-07-02 11:19:10
# FileName: datetime_process.py
# Software: PyCharm
'''

from dateutil import rrule
import datetime


# 算两个时间的月数
def months_calculate(begin, end):
    begin += '-01'
    end += '-01'
    d1 = datetime.datetime.strptime(begin,'%Y-%m-%d')
    d2 = datetime.datetime.strptime(end,'%Y-%m-%d')
    # d2 = datetime.date(2017, 4)
    months = rrule.rrule(rrule.MONTHLY, dtstart=d1, until=d2).count()
    return months


# 算两个时间的天数
def days_calculate(begin, end):
    begin = begin.split('-')
    end = end.split('-')
    d = int(begin[2])
    m = int(begin[1])
    y = int(begin[0])
    # difference in day
    dd = int(end[2])
    # difference in month
    dm = int(end[1])
    # difference in year
    dy = int(end[0])
    begin = datetime.date(y, m, d)
    end = datetime.date(dy, dm, dd)
    return (end - begin).days+1


# 算年数
def years_calculate(begin, end):
    begin = int(begin)
    end = int(end)
    return end-begin+1


# 生成连续的日期
def date_range(begin, end):
    ymd = "%Y-%m-%d"
    if len(begin) == 7:
        ymd = "%Y-%m"
    if len(begin) == 4:
        c = int(end) - int(begin)+1
        year = []
        for i in range(c):
            year.append(str(int(begin)+i))
        return sorted(year)
    dates = []
    dt = datetime.datetime.strptime(begin, ymd)
    date = begin[:]
    while date <= end:
        dates.append(date)
        dt = dt + datetime.timedelta(1)
        date = dt.strftime(ymd)
    return sorted(set(dates))


# 判断哪个时间大
def mix_min_check(start, end):
    a = int(start.replace('-', ''))
    b = int(end.replace('-', ''))
    if a > b:
        return False
    return True


print(days_calculate('2021-07-03', '2021-06-29'))

print(get_diff_days('2021-08-25 08:30:00', '2021-08-27 17:00:00'))

print(get_current_month_start_end())
print(get_last_month_start_end())


start_date, end_date = get_month_start_end("2021-10")
print(start_date.strftime('%Y-%m-%d'), end_date.strftime('%Y-%m-%d'))

print(get_yesterday())

print(get_dates("2021-02-01", "2021-02-15"))